<?php
/**
 * @package		简易CMS
 * @subpackage	应用程序基本处理类
 * @version		$Id: class_application.php 23 2012-06-11 18:04:03Z htmambo@gmail.com $
 * @author		Hoping
 * @copyright	Copyright (C) 2010 Hoping Software Studio.
 */

class application {
	var $cachelist = array();
	var $setting = array();
	var $get = array();
	var $post = array();

	/**
	 * @var Plugins
	 */
	var $plugin;
	/**
	 * 系统语言包处理
	 * @var cmsLanguage
	 */
	var $lang;
	function __construct(){
		$this->application();
	}
	function application(){
		global $_G, $mainframe;
		require_once libfile('function/misc');
		$mainframe->cachelist = $this->cachelist;
		$mainframe->init();
		if(!$_G['gp_task']) $_G['gp_task'] = 'index';
		if($this->allowaction){
			if($this->allowaction && !in_array($_G['gp_task'], $this->allowaction)) {
				showmessage('Undefined action, please return.', NULL);
			}
		}
		if(!$_G['basescript'])$_G['basescript'] = $_G['gp_option'];
		define('CURMODULE', $_G['gp_task']);
		$mainframe->lang->load($_G['basescript']);
		$this->lang = &$mainframe->lang;
		$this->setting = $_G['setting'];
		$this->load('function/'.CURSCRIPT);
		if($_G['setting']['enablesubdomain']){
			/* 获取二级域名 */
			$subdomain = get_subdomain();

			/* 没有二级域名，不解析 */
			if ($subdomain !== false) {
				$domain = get_domain();
				/* 解析对应的二级域名到对应的分类上 */
				$cate_id = get_subdomain_cate_id($subdomain);
				if($cate_id){
					define('SUB_DIR', $subdomain);
					if(!$_GET['task']){
						$_GET['task'] = 'list';
						$_GET['catid'] = $cate_id;
						$_G['gp_task'] = 'list';
						$_G['gp_catid'] = $cate_id;
					}
				}
			}
		}
		if($_GET){
			foreach((array)$_GET as $k=>$v){
				$this->get[$k] = $_G['gp_'.$k];
			}
		}
		if($_POST){
			foreach((array)$_POST as $k=>$v){
				$this->get[$k] = $_G['gp_'.$k];
			}
		}
		require_once libfile('class/plugins');
		$this->plugin = new Plugins();
	}
	function ajaxReturn($status = 1, $msg = 'yes'){
		exit(json_encode(array('status'=>$status, 'msg'=>$msg)));
	}
	function run(){
		global $_G;
		if($_G['gp_task']) $method = 'on'.$_G['gp_task'];
		else $method = 'onIndex';
		$this->$method();
	}

	function load($file, $path = ''){
		$fn = libfile($file, $path, false);
		if($fn) require_once $fn;
	}

	/**
	 * 获取当前页的数据库起始行号
	 *
	 * @param 当前页码 $page
	 * @param 总条目数 $totalnum
	 * @param 每页条目数 $ppp
	 * @return 数组 起始行号，总页数，每页条目数
	 */
	function page_get_start($page, $totalnum, $ppp = 0) {
		$page = intval($page);
		$ppp = intval($ppp);
		$totalnum = intval($totalnum);
		if(!$page) $page = 1;
		if(!$ppp) $ppp = 15;
		$totalpage = ceil($totalnum/$ppp);
		$page = max(1, min($totalpage, intval($page)));
		return array(($page-1)*$ppp, $totalpage, $ppp);
	}

	function addCustom($type = 'style', $handle, $src = false, $deps = array(), $ver = false, $media = 'all'){
		global $_G;
		if(!$src) return;
		$key = md5($src);
		$_G['custom'][$type][$key] = array(
			'id' => $handle,
			'src' => $src,
			'media' => $media,
			'ver' => $ver
		);
	}
}
